//Handin 5, due 10/10 2011
//Author Johan Sivertsen

package handin_5;

import java.util.ArrayList;

public class MagicSquare {
	//Instance variables
	private int n=0;   //Sidelength
	private int[][] square;  //2d array for holding our magic square candidate.
	private int r =0; //row posistion
	private int c =0; //collum postition
	private int checkValue=0; //the sum to check the square against.
	//Constructors
	public MagicSquare(int sideLength)
	{
		n = sideLength;
		square = new int[n][n];
	}
	
	
	//Methods
	/**
	 * Add an element to the square candidate.
	 * @param e
	 */
	public void addElement(int e)
	{
		
		if (c<n)
		{
			square[r][c]=e;
			c++;			
		}
		else if(r<n)
		{
			c=0;
			r++;
			square[r][c]=e;
			c++;
		}
		else 
		{
			System.out.println("The square is full, cannot add element");
		}
		
	}
	
	
	/**
	 * Return an ArrayList<Integer> of unused elements.
	 * @return
	 */
	public ArrayList<Integer> unusedNumbers()
	{
		ArrayList<Integer> unUsed = new ArrayList<Integer>();

		for(int i=1;i<=n*n;i++)
		{
			unUsed.add(i);
		}
		
		for (int k=0;k<n;k++)
		{
			for (int j=0;j<n;j++)
			{
				if (square[k][j] != 0)
				{
					unUsed.remove((Integer)square[k][j]);
				}
			}
		}
		return unUsed;
		
	}
	
	/**
	 * Returns true when the number of elements equals n^2. Othervise false.
	 * @return
	 */
	public boolean correctNumberOfElements()
	{
		for (int k=0;k<n;k++)
		{
			for (int j=0;j<n;j++)
			{
				if (square[k][j] == 0)
				{
					return false;
				}
			}
		}
		return true;
	}
	
	/**
	 * Prints the square candidate to the console with added spacing for your viewing pleasure.
	 */
	public void printSquare()
	{
		for (int k=0;k<n;k++)
		{
			for (int j=0;j<n;j++)
			{
				System.out.print(square[k][j]+" ");
			}
			System.out.print("\n");
		}
		
	}
	
	/**
	 * Tests whether the square is magic.
	 * @return True when magic, false otherwise.
	 */
	public boolean isMagic()
	{

		//Getting a checksum from the first row
		int tempsum=0;
		for (int k=0;k<n;k++)
		{
			tempsum+=square[k][0];
		}
		checkValue=tempsum;
		//Checking the rows
		for (int k=0;k<n;k++)
		{
			int temp=0;
			for (int j=0;j<n;j++)
			{
				temp+=square[k][j];
			}
			if (temp!=checkValue)
			{
				return false;
			}
		}
		//Checking the collums
		for (int k=0;k<n;k++)
		{
			int temp=0;
			for (int j=0;j<n;j++)
			{
				temp+=square[j][k];
			}
			if (temp!=checkValue)
			{
				return false;
			}
		}
	
		
		//Checking the diagonals
		//Top-Left to bottom-right
		for (int k=0;k<1;k++)
		{
			int temp=0;
			for (int j=0;j<n;j++)
			{
				temp+=square[j][j];
			}
			if (temp!=checkValue)
			{
				return false;
			}
		}
		//Top-Right to bottom-left
		for (int k=0;k<1;k++)
		{
			int temp=0;
			int rev=n-1;
			for (int j=0;j<n;j++)
			{
				temp+=square[j][rev];
				rev--;
			}
			if (temp!=checkValue)
			{
				return false;
			}
		}
		
		return true;
		
	}
	
		

}


